package secondStage.part5.网络编程.TCP通信程序;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @ClassName: [ServerTcpReceiveTest]
 * @Description: TCP通信程序，接收信息，服务器:接收到的数据写入文华文件
 * @Author: [黄劲松]
 * @CreateDate: [2021-04-06]
 * @Version: [v1.0]
 */
// 服务器:接收到的数据写入文华文件
public class ServerTcpReceiveTest2 {
    public static void main(String[] args) throws IOException {
        //创建, 服务器端的Socket对象(ServerSocket)
        ServerSocket ss = new ServerSocket (8080);
        //监听客户端连接，返回一个Socket对象
        Socket s = ss.accept();
        //接收数据
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        //把数据写入文本文件
        BufferedWriter bw = new BufferedWriter(new FileWriter("java.txt"));

        String line;
        while ((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        //释放资源.
        bw.close();
        ss.close();
    }
}
